package com.shycloud.mido.common.pay.wxpaynew;

import com.shycloud.mido.common.pay.wxpay.WXUtils.MyConfig;
import com.shycloud.mido.common.pay.wxpay.WXUtils.WXPayNative;
import com.shycloud.mido.organ.api.enums.MessageCodeEnum;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static com.shycloud.mido.common.pay.wxpay.WXH5Pay.getRandomString;

/**
 * @author Xiaoxing Yu
 * @date 2020/9/22
 **/
public class WxpayTest {

	public static void main(String[] args) throws Exception {
		BigDecimal payTotal = new BigDecimal(1);

		int price100 =0;
		MyConfig config = new MyConfig();
		Map<String, String> resp = new HashMap<>();
		//订单号：随机生成时间戳
		String outTradeNo = String.valueOf(System.currentTimeMillis()) ;
		if (payTotal != null ){
			price100 = payTotal.multiply(new BigDecimal(100)).intValue();
		}else {
//			return new R<>(resp.get("err_code"),"二位码生成失败，请联系管理员");
			System.out.println(resp.get("err_code"));
			System.out.println("二位码生成失败，请联系管理员");
		}
		//回调地址：微信返回参数时用到
//		String notifyUrl = TrainCommonEnum.WX_NOTIFY_URL.getCode();
		//待用
		String notifyUrl = MessageCodeEnum.WX_NOTIFY_URL.getMessage();
		String random1 = getRandomString(32);
		WXPayNative wxpay = new WXPayNative(config);
//		dto.setOrderCode(outTradeNo);
		Map<String, String> data = new HashMap<String, String>();
//		dto.getPackageName();
//		if (null != dto.getPackageName() && !"".equals(dto.getPackageName())){
//			data.put("body", dto.getPackageName());
//		}else {
			data.put("body", "自定义套餐");
//		}
		data.put("out_trade_no", outTradeNo);
		data.put("device_info", "WEB");
		data.put("fee_type", "CNY");
		data.put("total_fee", String.valueOf(price100));
		data.put("spbill_create_ip", "192.168.0.171");	//PayCommonUtil.getIPAddress(request)
//		data.put("notify_url", "http://joymidopay.free.idcfengye.com/organization/pay/wxNativeNotify");
		data.put("notify_url", notifyUrl);
		// 此处指定为扫码支付
		data.put("trade_type", "NATIVE");
		data.put("product_id", random1);
		try {
			resp = wxpay.unifiedOrder(data);
			System.out.println(resp);
		} catch (Exception e) {
			e.printStackTrace();
//			return new R<>(resp.get("err_code"),"二位码生成失败，请联系管理员");
			System.out.println(resp.get("err_code"));
			System.out.println("二位码生成失败，请联系管理员");
		}
		if ("FAIL".equals(resp.get("result_code"))){
//			return new R<>(resp.get("err_code"),"二位码生成失败，请联系管理员");
			System.out.println(resp.get("err_code"));
			System.out.println("二位码生成失败，请联系管理员");
		}else{
//			orgOrderService.advanceOrder(dto);
//			return new R<>(resp.get("code_url"));
			System.out.println("生成二维码URL: " + resp.get("code_url"));
		}
	}

}
